package com.li.Demo;

import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**  
* 类说明   
*  
* @author ****  
* @date 2018年3月20日  新建  
*/
public class HttpLogin {
		public static void main(String[] args) {
	        // 登陆 Url
	        String loginUrl = "http://localhost:8082/SCZQ/user/login.do";
	        // 需登陆后访问的 Url
	        String dataUrl = "http://localhost:8082/SCZQ/static/pages/user/identify_Code.jsp";
	        
	        String login = "http://localhost:8082/SCZQ/user/loginUI.do";
	        HttpClient httpClient = new HttpClient();
	 
	        // 模拟登陆，按实际服务器端要求选用 Post 或 Get 请求方式
	        PostMethod postMethod = new PostMethod(loginUrl);
	 
	        // 设置登陆时要求的信息，用户名和密码
	        NameValuePair[] data = { new NameValuePair("loginName", "lzadmin"), new NameValuePair("password", "654321") ,new NameValuePair("usercaptcha","9097")};
	        postMethod.setRequestBody(data);
	        try {
	            // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
	            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
	            int statusCode=httpClient.executeMethod(postMethod);
	                             
	            // 获得登陆后的 Cookie
	            Cookie[] cookies = httpClient.getState().getCookies();
	            StringBuffer tmpcookies = new StringBuffer();
	            for (Cookie c : cookies) {
	                tmpcookies.append(c.toString() + ";");
	                System.out.println("cookies = "+c.toString());
	            }
	            if(statusCode==200){//重定向到新的URL
	                System.out.println("模拟登录成功");
	                // 进行登陆后的操作
	                GetMethod getMethod = new GetMethod(dataUrl);
	                // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
	                getMethod.setRequestHeader("cookie", tmpcookies.toString());
	                // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
	                // 例如，referer 从哪里来的，UA 像搜索引擎都会表名自己是谁，无良搜索引擎除外
//	                postMethod.setRequestHeader("Referer", "http://passport.mop.com/");
//	                postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
	                httpClient.executeMethod(getMethod);
	                // 打印出返回数据，检验一下是否成功
	                InputStream is = getMethod.getResponseBodyAsStream();
	        		FileOutputStream fos = new FileOutputStream("C:/11.jpg");
	        		byte[] b = new byte[1024];
	        		while ((is.read(b)) != -1) {
	        			fos.write(b);
	        		}
	        		fos.flush();
	        		is.close();
	        		fos.close();
	            }
	            else {
	                System.out.println("登录失败");
	            }
	        }
	        catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	}
  